package com.trt.workflow;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 谭仁涛
 * @description UEL表达式
 * @date 2022/2/20 10:11
 */
@SpringBootTest
public class Part6_UEL {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    //启动流程实例带参数，执行执行人  对应流程图Part6_UEL_V1.bpmn
    @Test
    public void initProcessInstanceWithArgs() {
        // 流程变量 ${ZhiXingRen}
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("ZhiXingRen", "wukong"); // 可以多个参数
        //variables.put("ZhiXingRen2", "aaa");
        //variables.put("ZhiXingRen3", "wukbbbong");

        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey(
                        "myProcess_UEL_V1",
                        "bkey004",
                        variables);
        System.out.println("流程定义ID : " + processInstance.getProcessDefinitionId());
    }

    @Test
    public void initProcessInstance(){
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_UEL_V2", "bKey005");
        System.out.println("流程定义ID : " + processInstance.getProcessDefinitionId());
    }

    //完成任务带参数，指定流程变量测试  对应流程图Part6_UEL_V2.bpmn myProcess_UEL_V2
    @Test
    public void completeTaskWithArgs() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("pay", "101");
        taskService.complete("ee813824-91fa-11ec-964e-00ff6fa8c6c1", variables);
        System.out.println("完成任务");
    }

    //启动流程实例带参数，使用实体类 对应流程图Part6_UEL_V3.bpmn  myProcess_UEL_V3
    @Test
    public void initProcessInstanceWithClassArgs() {
        UEL_POJO uelPojo = new UEL_POJO();
        uelPojo.setZhixingren("bajie");

        // 流程变量
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("uelpojo", uelPojo);

        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey(
                        "myProcess_UEL_V3",
                        "bkey006",
                        variables);
        System.out.println("流程定义ID : " + processInstance.getProcessDefinitionId());
    }

    //任务完成环节带参数，指定多个候选人  对应流程图Part6_UEL_V3.bpmn  myProcess_UEL_V3
    @Test
    public void initProcessInstanceWithCandiDateArgs() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("houxuanren", "wukong,tangseng");
        taskService.complete("84fd2a00-91fd-11ec-80ec-00ff6fa8c6c1", variables);
        System.out.println("完成任务");

    }

    //直接指定流程变量
    @Test
    public void otherArgs() {
        runtimeService.setVariable("84fd2a00-91fd-11ec-80ec-00ff6fa8c6c1","pay","101");
//        runtimeService.setVariables();
//        taskService.setVariable();
//        taskService.setVariables();

    }

    //局部变量
    @Test
    public void otherLocalArgs() {
        runtimeService.setVariableLocal("84fd2a00-91fd-11ec-80ec-00ff6fa8c6c1","pay","101");
//        runtimeService.setVariablesLocal();
//        taskService.setVariableLocal();
//        taskService.setVariablesLocal();
    }

}
